package com.yugao.fintech.vctgone.admin.controller;

import com.yugao.fintech.vctgone.admin.pojo.query.LogQuery;
import com.yugao.fintech.vctgone.admin.pojo.vo.LogVO;
import com.yugao.fintech.vctgone.admin.service.LogService;
import com.yugao.fintech.vctgone.auth.api.dto.LogDTO;
import com.yugao.fintech.vctgone.mybatisplus.util.PageUtil;
import com.yugao.fintech.vctgone.web.util.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log")
@Tag(name = "日志管理")
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class LogController {
    private final LogService logService;

    @GetMapping
    @Operation(summary = "获取日志")
    @PreAuthorize("hasAuthority('sys_log_query')")
    public ResultVO<PageUtil<LogVO>> getLogs(LogQuery query) {
        return ResultVO.success(logService.getLogs(query));
    }

    @PostMapping
    @Operation(summary = "保存日志")
    public ResultVO<?> saveLog(@RequestBody LogDTO dto) {
        logService.saveLog(dto);
        return ResultVO.success();
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除日志")
    @PreAuthorize("hasAuthority('sys_log_delete')")
    public ResultVO<?> deleteLogs(@PathVariable String ids) {
        logService.deleteLogs(ids);
        return ResultVO.success();
    }

    @GetMapping("/export")
    @Operation(summary = "导出日志")
    @PreAuthorize("hasAuthority('sys_log_export')")
    public void export(LogQuery query, HttpServletResponse response) {
        logService.export(query, response);
    }
}
